﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class WBBSTController
    {
        [ChildActionOnly]
        public ActionResult SSTKNR(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.IsVAFReady = MABF.IsVAFReady;
            ViewBag.IsSTKNRReady = MABF.IsSTKNRReady;
            ViewBag.IsEXORDReady = MABF.IsEXORDReady;
            ViewBag.ReturnUrl = returnUrl;
            
            if (!MABF.IsSTKNRReady)
            {
                INFO = "请扫描有效的单号";
            }
            return PartialView(MABF.CWBB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SSTKNR(WBBSTModel model, string returnUrl)
        {
            try
            {
                ModelState.Clear();
                if (model.WBBSTKNR.Trim().Length < 3)
                {
                    return View();
                }
                MABF.STKNR = model.WBBSTKNR.PadRight(12);
                CHKITX(model);
            }
            catch (Exception ex)
            {
                INFO = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void CHKITX(WBBSTModel model)
        {
            PM["QITABLE"] = MABF.MAM.MKEY;
            PM["STKNR"] = MABF.STKNR;
            PM["ART"] = MABF.ART;
            PM["ABAUF"] = MABF.ABPAUF;
            SResult = VHP.VWBB.QITXWBB(PM);
            foreach (DataRow row in SResult.Tables[0].Copy().Rows)
            {
                MABF.IsVAFReady = true;
                MABF.IsEXORDReady = true;
                MABF.IsSTKNRReady = true;
                MABF.CWBB = new WBBSTModel();
                MABF.CWBB.IsERKReady = false;
                MABF.CWBB.WBBSTKNR = row["WBBSTKNR"].ToString();
                MABF.CWBB.RWKRS = row["WBBRWKRS"].ToString();
                MABF.CWBB.RWLFD = row["WBBRWLFD"].ToString();
                MABF.CWBB.RWPOS = row["WBBPOS"].ToString();

                MABF.CWBB.dwWB = SResult.Tables[0].Clone().Rows.Add();
                MABF.CWBB.dwWB.ItemArray = row.ItemArray;
                MABF.CWBB.WHP = "01CP01      ";
                MABF.CWBB.ART = "F" + row["WBBART"].ToString() + row["WBBVAR"].ToString().PadRight(3) + row["WBBDESS"].ToString().PadRight(10) + row["WBBFARBE"].ToString().PadRight(10) + row["WBBCOLORIT"].ToString().PadRight(10) + row["WBBBESCHICH"].ToString().PadRight(10);
                DataSet sTemp = VHP.VWBB.QWBFVERVR(row); 
                foreach (DataRow ro in sTemp.Tables[0].Rows)
                {
                    MABF.CWBB.ERVER = int.Parse(ro["ERVER"].ToString());
                } 
                break;
            }
            INTEXVALIDATE();
        }
        public void INTEXVALIDATE()
        {
            if (MABF.IsSTKNRReady)
                return;
            PM["STKNR"] = MABF.STKNR;
            PM["ART"] = MABF.ART;
            PM["ABAUF"] = MABF.STKNR;
            SResult = QEXTSOURCE(PM);
            if (SResult.Tables[0].Rows.Count == 0)
                return;

            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                Helper.GetSTime(ref date, ref time, ref stime);
                MABF.WFART = "F" + Helper.Left(stime + row["WBBART"].ToString().PadRight(20), 20)
                       + Helper.Left(row["WBBVAR"].ToString().PadRight(2), 2)
                       + Helper.Left(row["WBBFARBE"].ToString().PadRight(10), 10)
                       + Helper.Left(row["WBBBESCHICH"].ToString().PadRight(10), 10)
                        + Helper.Left(row["WBBDESS"].ToString().PadRight(10), 10)
                       + Helper.Left(row["WBBCOLORIT"].ToString().PadRight(10), 10)
                          + "".PadRight(7)
                       + Helper.Left(row["WABEZ"].ToString().PadRight(20), 20);
                MABF.WAART = MABF.WFART.Substring(0, 23);
                MABF.EXORD = Helper.Left(row["PRODORDER"].ToString().PadRight(11), 11)
                   + MABF.WFART
                    + Helper.Left(row["KDKUNDNA"].ToString().PadRight(8), 8)
                    + Helper.Left(row["LABELTYP"].ToString().PadRight(20), 20)
                     + Helper.Left(row["PARTIE"].ToString().PadRight(10), 10);
                break;
            }
            MABF.IsVAFReady = false;
            MABF.IsEXORDReady = false;
        }
        private DataSet QEXTSOURCE(Dictionary<string, object> pm)
        {
            string _ABAUF = pm["ABAUF"].ToString();
            string _ART = pm["ART"].ToString();
            //string sql = " AND WBBART||WBBVAR||WBBFARBE LIKE '" + _ART.Trim() + "%'" + " AND WBBRWKRS||WBBRWLFD||WBBPOS LIKE '" + _ABAUF.Trim() + "%'";
            string sql = " AND WBBART||WBBVAR||WBBFARBE LIKE '" + _ART.Trim() + "%'";
            SResult = VHP.VWBB.QWBEX91(sql);
            return SResult;
        }

    }
}
